/*
 * File: MessageSetter.java Author: Robert Bittle <guywithnose@gmail.com>
 */
package servlets;

import java.io.IOException;
import java.util.GregorianCalendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import faceBook.*;

/**
 * The Class MessageSetter.
 */
public class MessageSetter extends AuthenticationServlet
{

  /*
   * (non-Javadoc)
   * 
   * @see
   * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
   * javax.servlet.http.HttpServletResponse)
   */
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException
  {
    resp.addHeader("Content-Type", "application/json");

    if (req.getParameter("id") == null || req.getParameter("userid") == null
        || req.getParameter("accessToken") == null)
    {
      resp.getWriter().write(errorJson("Authentication failed."));
      return;
    }
    long id = Long.valueOf(req.getParameter("id"));
    Message message = new Message(id);

    if (!authenticate(Long.valueOf(req.getParameter("userid")),
            req.getParameter("accessToken")))
    {
      resp.getWriter().write(errorJson("Authentication failed."));
      return;
    }

    if ("true".equals(req.getParameter("delete")))
    {
      if(message.getConfirmations() != "")
      {
        resp.getWriter().print(errorJson("Message already sent"));
        return;
      }
      message.delete();      
      resp.getWriter().print(successJson());
    } else
    {
      if (id == 0)
      {
        User user = new User(Long.valueOf(req.getParameter("userid")));
        if(user.getCustomMessageCount() <= 0)
        {
          resp.getWriter().print(errorJson("buyMessages"));
          return;
        }
        message = new Message();
        if (req.getParameter("userid") != null)
          message.setUser(user);
        if (req.getParameter("friendid") != null)
        {
          String[] friends = req.getParameter("friendid").split(",");
          for (int i = 0; i < friends.length; i++)
            message.addFriend(new User(Long.valueOf(friends[i])));
        }
        if (req.getParameter("type") != null)
          message.setType(req.getParameter("type"));
        if (req.getParameter("holiday") != null)
          message.setHoliday(Long.valueOf(req.getParameter("holiday")));
        if (req.getParameter("date") != null
            && req.getParameter("month") != null
            && req.getParameter("year") != null)
        {
          int date = Integer.valueOf(req.getParameter("date"));
          int month = Integer.valueOf(req.getParameter("month"));
          int year = Integer.valueOf(req.getParameter("year"));
          message.setSendDate(new GregorianCalendar(year, month, date)
              .getTimeInMillis());
        }
      }
      message.setMessage(req.getParameter("message"));
      message.save();
      try
      {
        JSONObject jo = new JSONObject();
        jo.put("id", message.getID());
        jo.put("success", true);        
        resp.getWriter().print(jo.toString());
      } catch (JSONException e)
      {
        e.printStackTrace();
      }
    }
  }

}